
package com.cynoware.busappmas.util;

import com.cynoware.busappmas.util.ShellUtils.CommandResult;



public class GPIO {

    private int mNum; // GPIO number
    private final boolean pad_run = false;

    public GPIO(int num) {
        mNum = num;
    }

    public void open() {
    	if (!pad_run){
    		ShellUtils.execCommand(String.format("echo %d > /sys/class/gpio/export", mNum), true);
    		ShellUtils.execCommand(String.format("echo out > /sys/class/gpio/gpio%d/direction", mNum), true);
    	}
    	
 
    }
    
    public void openAsIn(){
    	if (!pad_run){
    		ShellUtils.execCommand(String.format("echo %d > /sys/class/gpio/export", mNum), true);
    		ShellUtils.execCommand(String.format("echo in > /sys/class/gpio/gpio%d/direction", mNum), true);
    	}
    }

    public void close() {
    	if (!pad_run){
    		ShellUtils.execCommand(String.format("echo %d > /sys/class/gpio/unexport", mNum), true);
    	}
    }

    public void setValue(int value) {
    	if (!pad_run){
    		ShellUtils.execCommand(String.format("echo %d > /sys/class/gpio/gpio%d/value", value, mNum), true);
    	}
    }

    public int getValue() {
    	int ret = 0;
    	if (!pad_run){
    		CommandResult result = ShellUtils.execCommand(String.format("cat /sys/class/gpio/gpio%d/value", mNum), true);
    		if(result==null) {
    			ret = 3;
    		}else {
    			ret = Integer.parseInt(result.successMsg);
    		}
        }
    	return ret;
    }
}
